package com.itheima.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.domain.SysRole;
import com.itheima.youdianda.domain.SysUser;
import com.itheima.youdianda.dto.PageDto;
import com.itheima.youdianda.dto.SysUserDto;
import com.itheima.youdianda.mapper.SysUserMapper;
import com.itheima.youdianda.service.SysRoleMenuService;
import com.itheima.youdianda.service.SysRoleService;
import com.itheima.youdianda.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户管理处理层
 */
@Service
@Slf4j
@Transactional
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Value("${YouDianDa.salt}")
    private String salt;//读取固定的盐值

    /**
     * 权限管理-管理员管理/用户分页查询
     *
     * @param page
     * @param limit
     * @param loginName
     * @param status
     * @param phone
     * @return
     */
    @Override
    public PageDto<SysUserDto> sysUserList(Integer page, Integer limit, String loginName, Integer status, String phone) {
//      分页查询器
        Page<SysUser> pageInfo = new Page<>(page, limit);
        Page<SysUserDto> dtoPageInfo = new Page<>();
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
//      处理服务端是否输入查询条件
        queryWrapper.eq(null != loginName, SysUser::getLoginName, loginName);
        queryWrapper.eq(null != status, SysUser::getStatus, status);
        queryWrapper.eq("" != phone, SysUser::getPhone, phone);
        queryWrapper.orderByDesc(SysUser::getCreateDate);
        this.page(pageInfo, queryWrapper);
        BeanUtils.copyProperties(pageInfo, dtoPageInfo, "records");
        List<SysUser> records = pageInfo.getRecords();
        //封装dtoPageInfo中的records数据集
        List<SysUserDto> dtoList = records.stream().map((item) -> {
            SysUserDto sysUserDto = new SysUserDto();
            BeanUtils.copyProperties(item, sysUserDto, "roleId");
            String[] ids = item.getRoleId().split(",");
            List<String> roleIds = new ArrayList<>();
            for (String id : ids) {
                roleIds.add(id);
            }
            List<String> roleNames = new ArrayList<>();
            for (String roleId : roleIds) {
                SysRole sysRole = sysRoleService.getById(roleId);
                if(sysRole!=null){
                    String roleName = sysRole.getName();
                    roleNames.add(roleName);
                }
            }
            sysUserDto.setRole_id(roleIds);
            sysUserDto.setRoleName(roleNames);
            return sysUserDto;
        }).collect(Collectors.toList());
        dtoPageInfo.setRecords(dtoList);
        PageDto<SysUserDto> sysUserDtoPageDto = new PageDto<>();
        BeanUtils.copyProperties(dtoPageInfo,sysUserDtoPageDto,"records");
        sysUserDtoPageDto.setValue(dtoPageInfo);
        return sysUserDtoPageDto;
    }

    /**
     * 逻辑删除
     *
     * @param id
     */
    @Override
    public void sysUserDel(String id) {
        this.removeById(id);
    }

    /**
     * 保存用户
     *
     * @param sysUserDto
     */
    @Override
    public void sysUserAdd(SysUserDto sysUserDto) {
        List<String> role_id = sysUserDto.getRole_id();
        String roleId ="";
        for (String s : role_id) {
            roleId+=s+",";
        }
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto, sysUser, "role_id", "roleName");
        sysUser.setPassword(DigestUtils.md5DigestAsHex((sysUserDto.getPassword()+salt).getBytes()));
        sysUser.setRoleId(roleId);
        this.save(sysUser);
    }

    /**
     * 用户详情
     * @param id
     * @return
     */

    @Override
    public SysUserDto sysUserRowInfo(String id) {
        //通过id拿到用户
        SysUser sysUser = this.getById(id);
        String roleId = sysUser.getRoleId();
        String[] ids = roleId.split(",");
        List<String> roleIds = new ArrayList<>();
        Collections.addAll(roleIds, ids);
        SysUserDto sysUserDto = new SysUserDto();
        BeanUtils.copyProperties(sysUser, sysUserDto);
        sysUserDto.setRole_id(roleIds);
        return sysUserDto;
    }

    /**
     * 修改用户
     * @param sysUserDto
     */
    @Override
    public void sysUserUpdate(SysUserDto sysUserDto) {
        SysUser sysUser = new SysUser();
//        String id=sysUserDto.getId();
//        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(SysUser::getId,id);
//        SysUser user = this.getOne(queryWrapper);
        BeanUtils.copyProperties(sysUserDto,sysUser,"role_id");
//        sysUser.setId(sysUserDto.getId());
        List<String> role_id = sysUserDto.getRole_id();
        String ids = role_id.toString();

        sysUser.setRoleId(ids);
        sysUser.setPassword(DigestUtils.md5DigestAsHex((sysUserDto.getPassword()+salt).getBytes()));
        this.updateById(sysUser);

        System.out.println("1");
    }

}
